import Vue from 'vue';
import Vuex from 'vuex';
import { api } from '../Axios';
import _ from 'lodash';

Vue.use(Vuex);
import cusInfo from './cusInfo.js';
import playerSet from './playerSet.js';
import itemsModule from './itemsModule.js';
import map from './mapModule.js';

// import backpackModule from './backpackModule.js';
// import recipesModule from './recipesModule.js';

export default new Vuex.Store({
  modules: {
    cusInfo,
    // chatRoom,
    itemsModule, // 道具模块
    // backpackModule, //背包模块
    // recipesModule, //配方模块
    // crafting, //制作模块,调用前面三个模块
    map,
  },
  state: {
    TOKEN: null,
    msg: { name: 'a123' },
    MAP: {
      _AMapSecurityConfig: {},
      securityJsCode: 'd48e7f2bd17c16b4cb757cffbb694b90', //「你申请的安全密钥」
      key: '2b05e7179f91bbd5c2c8b9780493cf28', // 申请好的Web端开发者Key，首次调用 load 时必填
    },
    // prettier-ignore
    订单类型: [
      {label: '全部',value: {},},
      {label: '待付款',value: {is付款: false,is发货: false,is收货: false,},},
      {label: '待发货',value: {is付款: true,is发货: false,is收货: false,},},
      {label: '待收货',value: {is付款: true,is发货: true,is收货: false,},},
      {label: '已收货',value: {is付款: true,is发货: true,is收货: true,},},
    ],
    // prettier-ignore
    取货方式: [],
    归还方式: [],
  },
  getters: {
    getValue: (state) => (key) => {
      return state[key];
    },
    getTOKEN: (state) => {
      if (state.TOKEN === null) {
        state.TOKEN = localStorage.getItem('TOKEN') || null;
      }
      return state.TOKEN;
    },
    optsFindByPredicate: (state) => {
      return (tarKey, predicate) => {
        let tar = state[tarKey];
        let list = tar.map((v, i) => {
          return _.get(v, 'item');
        });
        let idx = _.findIndex(list, predicate);
        if (idx >= 0) {
          return tar[idx].value;
        } else {
          return null;
        }
      };
    },
    optsFindByValue: (state) => {
      return (tarKey, value) => {
        let tar = state[tarKey];
        let item = _.find(tar, { value: value });
        return _.get(item, 'item');
      };
    },
  },
  mutations: {
    setValue: (state, obj) => {
      state[obj.key] = obj.value;
    },
    setLocal: (state, value) => {
      state.key = value;
      localStorage.setItem('key', value);
    },
    setOpts: (state, { arr, tarKey, labelKey, valueKey, needClear }) => {
      if (!_.has(state, tarKey) || !_.isArray(state[tarKey])) return;
      let tar = state[tarKey];
      if (needClear) tar.splice(0, tar.length);
      arr.forEach((v, i) => {
        let obj = {
          label: _.get(v, labelKey),
          value: _.get(v, valueKey),
          item: v,
        };
        tar.push(obj);
      });
    },
  },
  actions: {
    // ({ commit, dispatch, state, getters, rootState },payload)
    // this.$store.dispatch('actionName', payload)
    fetchEditableOptions: (ctx, payload) => {
      api
        .fetchEditableOptions()
        .then((res) => {
          if (res.code != 200 || !res.data) return Promise.reject();
          ctx.commit('setOpts', {
            arr: res.data['取货方式'],
            tarKey: '取货方式',
            labelKey: 'label',
            valueKey: 'value',
            needClear: true,
          });
          ctx.commit('setOpts', {
            arr: res.data['归还方式'],
            tarKey: '归还方式',
            labelKey: 'label',
            valueKey: 'value',
            needClear: true,
          });
          console.log(res);
        })
        .catch((err) => {
          console.error(err);
        })
        .finally((_) => {});
    },
  },
});
